
********************************************************************************
********************************************************************************
*************************** THIS IS DO FILE 7 (IND)*****************************
********** THIS DO FILE Merges THE MODEL ESTIMATES WITH THE VISIT DATA *********
********************************************************************************
********************************************************************************

*Version: 2023 March 29


local date "$date"
cap mkdir `"$output/temp"'
cap mkdir `"$output/log/relax_IND_7_compute_CIP"'
cap mkdir `"$output/data"'
cap mkdir `"$output/model_estimation/relax_IND"'
cap mkdir `"$output/model_estimation/relax_IND/parameters"'

cap log close 
log using `"$output/log/relax_IND_7_compute_CIP/relax_IND_7_compute_CIP.smcl"', replace 

di "This run uses code version from 2023/03/29"



****************************************************************
****************************************************************
******************* CIP calculation by Size ********************
****************************************************************
****************************************************************
use  `"$output/temp/relax_IND_estimation_data_bin.dta"', clear

gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"



** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



//merge in model estimates 
merge m:1 visit_code_cat state pay_type_pooled size_indicator using `"$output/model_estimation/relax_IND/estimations_bin.dta"' , keepusing(claim_cost claim_cost_se) nogen 
replace claim_cost = . if claim_cost_se == .

replace claim_cost = 0 if n_S == .

gen indirect_cost = exp(claim_cost +continuation)
gcollapse (sum) indirect_cost*, by(visit_billing_id)
replace indirect_cost = ln(indirect_cost) + 0.5772156649

save `"$output/temp/relax_IND_cont_value_model_merged.dta"', replace 


//merge into visit level data 
use `"$output/data/$line_file"', clear 

gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"

// generate denial indicator 
egen final_paid = rowtotal(Y_?)
gen initial_denial = 1-Y_0
gen final_denial = 1-final_paid
bys visit_billing_id: gegen visit_initial_den = max(initial_denial)
bys visit_billing_id: gegen visit_final_den = max(final_denial)

// generate pi
bys visit_billing_id: gegen pi = sum(line_item_value)
assert pi >=0

// generate final denied amt 
egen Y_max = rowmax(Y_?)
assert Y_max == 0 | Y_max == 1
bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_max ==0
bys visit_billing_id: gegen denied_amt_final = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt_final = 0 if denied_amt_final == . 


// generate initial denied amt 
bys visit_billing_id: gegen denied_visit = min(Y_0)
replace denied_visit = 1-denied_visit

bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_0 ==0
bys visit_billing_id: gegen denied_amt = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt = 0 if denied_amt == . 

// generate RVU proxy of visit 
bys visit_billing_id: gegen RVU = sum(meanallowedMFFS) 

forvalues i = 1/5 {
	
	decode reason_`i', gen(reason_`i'_string)
	gen fineline_`i' = inlist(reason_`i'_string,"","45","1","2","3","253","223","23")
}
cap drop goodobs
egen allfine = rsum(fineline_?)
gen goodobs=allfine==5

bys visit_billing_id : gegen resubmitted = max(R_1)

// switch to visit level 
bys visit_billing_id: keep if _n == 1 
merge 1:1 visit_billing_id using `"$output/temp/relax_IND_cont_value_model_merged.dta"'
replace indirect_cost = 0 if _merge == 1
drop _merge 

gen CIP = denied_amt - indirect_cost

*****************************************************
*****************************************************
**** DROP NEGATIVE CIP ***
*****************************************************
*****************************************************
sum CIP, d 

drop if CIP < 0 | CIP>r(p99)  //dropping outlier


*****************************************
***** Generate Regression Variables *****
*****************************************
gen ln_pi = ln(pi)	// define allowed amount for visit j as pi

// generate tau and lost revenue 
gen tau = CIP/pi
gen tau_rf = (denied_amt_final/pi)
rename denied_amt_final lost_revenue
rename denied_amt initial_denied_amt

lab var lost_revenue "Final denied amt for a visit"
lab var tau_rf "Tau calculation for each visit if zero resubmission cost"
lab var tau "Tau calculation for each visit"
lab var initial_denied_amt "Amt that is initially denied"
lab var visit_initial_den "At least one line item initially denied"
lab var visit_final_den "At least one line item finally denied"
lab var CIP "CIP at visit level"


//SMALL GROUPS 
// summarize tau 
reg tau if pay_type_pooled ==1 & size_indicator ==1
est save `"$output/model_estimation/relax_IND/parameters/tau_Medicare_byreasons_smallgroups.ster"', replace

reg tau if pay_type_pooled ==2 & size_indicator ==1
est save `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_byreasons_smallgroups.ster"', replace

reg tau if pay_type_pooled ==3 & size_indicator ==1
est save `"$output/model_estimation/relax_IND/parameters/tau_Commercial_byreasons_smallgroups.ster"', replace

// summarize CIP 
reg CIP if pay_type_pooled ==1 & size_indicator ==1
est save `"$output/model_estimation/relax_IND/parameters/CIP_Medicare_byreasons_smallgroups.ster"', replace

reg CIP if pay_type_pooled ==2 & size_indicator ==1
est save `"$output/model_estimation/relax_IND/parameters/CIP_Medicaid_byreasons_smallgroups.ster"', replace

reg CIP if pay_type_pooled ==3 & size_indicator ==1
est save `"$output/model_estimation/relax_IND/parameters/CIP_Commercial_byreasons_smallgroups.ster"', replace

//LARGE GROUPS 
// summarize tau 
reg tau if pay_type_pooled ==1 & size_indicator ==2
est save `"$output/model_estimation/relax_IND/parameters/tau_Medicare_byreasons_largegroups.ster"', replace

reg tau if pay_type_pooled ==2 & size_indicator ==2
est save `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_byreasons_largegroups.ster"', replace

reg tau if pay_type_pooled ==3 & size_indicator ==2
est save `"$output/model_estimation/relax_IND/parameters/tau_Commercial_byreasons_largegroups.ster"', replace

// summarize CIP 
reg CIP if pay_type_pooled ==1 & size_indicator ==2
est save `"$output/model_estimation/relax_IND/parameters/CIP_Medicare_byreasons_largegroups.ster"', replace

reg CIP if pay_type_pooled ==2 & size_indicator ==2
est save `"$output/model_estimation/relax_IND/parameters/CIP_Medicaid_byreasons_largegroups.ster"', replace

reg CIP if pay_type_pooled ==3 & size_indicator ==2
est save `"$output/model_estimation/relax_IND/parameters/CIP_Commercial_byreasons_largegroups.ster"', replace


erase `"$output/temp/relax_IND_cont_value_model_merged.dta"'



****************************************************************
****************************************************************
*********** CIP calculation No Size Distinction ****************
****************************************************************
****************************************************************
use  `"$output/temp/relax_IND_estimation_data_bin.dta"', clear

gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"



** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



//merge in model estimates 
merge m:1 visit_code_cat state pay_type_pooled using `"$output/model_estimation/relax_IND/estimations_nosize_bin.dta"' , keepusing(claim_cost claim_cost_se) nogen 
replace claim_cost = . if claim_cost_se == .

replace claim_cost = 0 if n_S == .

gen indirect_cost = exp(claim_cost +continuation)
gcollapse (sum) indirect_cost*, by(visit_billing_id)
replace indirect_cost = ln(indirect_cost) + 0.5772156649

save `"$output/temp/relax_IND_cont_value_model_merged.dta"', replace 


//merge into visit level data 
use `"$output/data/$line_file"', clear 

gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"

// generate denial indicator 
egen final_paid = rowtotal(Y_?)
gen initial_denial = 1-Y_0
gen final_denial = 1-final_paid
bys visit_billing_id: gegen visit_initial_den = max(initial_denial)
bys visit_billing_id: gegen visit_final_den = max(final_denial)

// generate pi
bys visit_billing_id: gegen pi = sum(line_item_value)
assert pi >=0

// generate final denied amt 
egen Y_max = rowmax(Y_?)
assert Y_max == 0 | Y_max == 1
bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_max ==0
bys visit_billing_id: gegen denied_amt_final = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt_final = 0 if denied_amt_final == . 


// generate initial denied amt 
bys visit_billing_id: gegen denied_visit = min(Y_0)
replace denied_visit = 1-denied_visit

bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_0 ==0
bys visit_billing_id: gegen denied_amt = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt = 0 if denied_amt == . 

// generate RVU proxy of visit 
bys visit_billing_id: gegen RVU = sum(meanallowedMFFS) 

forvalues i = 1/5 {
	
	decode reason_`i', gen(reason_`i'_string)
	gen fineline_`i' = inlist(reason_`i'_string,"","45","1","2","3","253","223","23")
}
cap drop goodobs
egen allfine = rsum(fineline_?)
gen goodobs=allfine==5

bys visit_billing_id : gegen resubmitted = max(R_1)

// switch to visit level 
bys visit_billing_id: keep if _n == 1 
merge 1:1 visit_billing_id using `"$output/temp/relax_IND_cont_value_model_merged.dta"'
replace indirect_cost = 0 if _merge == 1
drop _merge 

gen CIP = denied_amt - indirect_cost

*****************************************************
*****************************************************
**** DROP NEGATIVE CIP ***
*****************************************************
*****************************************************
sum CIP, d 

drop if CIP < 0 | CIP>r(p99)  //dropping outlier


*****************************************
***** Generate Regression Variables *****
*****************************************
gen ln_pi = ln(pi)	// define allowed amount for visit j as pi

// generate tau and lost revenue 
gen tau = CIP/pi
gen tau_rf = (denied_amt_final/pi)
rename denied_amt_final lost_revenue
rename denied_amt initial_denied_amt

lab var lost_revenue "Final denied amt for a visit"
lab var tau_rf "Tau calculation for each visit if zero resubmission cost"
lab var tau "Tau calculation for each visit"
lab var initial_denied_amt "Amt that is initially denied"
lab var visit_initial_den "At least one line item initially denied"
lab var visit_final_den "At least one line item finally denied"
lab var CIP "CIP at visit level"


// summarize tau 

reg tau if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_IND/parameters/tau_Medicare_byreasons_allgroups.ster"', replace

reg tau if pay_type_pooled ==2
est save `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_byreasons_allgroups.ster"', replace

reg tau if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_IND/parameters/tau_Commercial_byreasons_allgroups.ster"', replace

// summarize CIP 

reg CIP if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_IND/parameters/CIP_Medicare_byreasons_allgroups.ster"', replace

reg CIP if pay_type_pooled ==2
est save `"$output/model_estimation/relax_IND/parameters/CIP_Medicaid_byreasons_allgroups.ster"', replace

reg CIP if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_IND/parameters/CIP_Commercial_byreasons_allgroups.ster"', replace


erase `"$output/temp/relax_IND_cont_value_model_merged.dta"'


****************************************************************
****************************************************************
******* CIP calculation No Size and Reason Distinction *********
****************************************************************
****************************************************************
use  `"$output/temp/relax_IND_estimation_data_bin.dta"', clear


** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



//merge in model estimates 
merge m:1 state pay_type_pooled using `"$output/model_estimation/relax_IND/estimations_nosize_noreason_bin.dta"' , keepusing(claim_cost claim_cost_se) nogen 
replace claim_cost = . if claim_cost_se == .

replace claim_cost = 0 if n_S == .

gen indirect_cost = exp(claim_cost +continuation)
gcollapse (sum) indirect_cost*, by(visit_billing_id)
replace indirect_cost = ln(indirect_cost) + 0.5772156649

save `"$output/temp/relax_IND_cont_value_model_merged.dta"', replace 


//merge into visit level data 
use `"$output/data/$line_file"', clear 

// generate denial indicator 
egen final_paid = rowtotal(Y_?)
gen initial_denial = 1-Y_0
gen final_denial = 1-final_paid
bys visit_billing_id: gegen visit_initial_den = max(initial_denial)
bys visit_billing_id: gegen visit_final_den = max(final_denial)

// generate pi
bys visit_billing_id: gegen pi = sum(line_item_value)
assert pi >=0

// generate final denied amt 
egen Y_max = rowmax(Y_?)
assert Y_max == 0 | Y_max == 1
bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_max ==0
bys visit_billing_id: gegen denied_amt_final = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt_final = 0 if denied_amt_final == . 


// generate initial denied amt 
bys visit_billing_id: gegen denied_visit = min(Y_0)
replace denied_visit = 1-denied_visit

bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_0 ==0
bys visit_billing_id: gegen denied_amt = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt = 0 if denied_amt == . 

// generate RVU proxy of visit 
bys visit_billing_id: gegen RVU = sum(meanallowedMFFS) 

forvalues i = 1/5 {
	
	decode reason_`i', gen(reason_`i'_string)
	gen fineline_`i' = inlist(reason_`i'_string,"","45","1","2","3","253","223","23")
}
cap drop goodobs
egen allfine = rsum(fineline_?)
gen goodobs=allfine==5

bys visit_billing_id : gegen resubmitted = max(R_1)

// switch to visit level 
bys visit_billing_id: keep if _n == 1 
merge 1:1 visit_billing_id using `"$output/temp/relax_IND_cont_value_model_merged.dta"'
replace indirect_cost = 0 if _merge == 1
drop _merge 

gen CIP = denied_amt - indirect_cost

*****************************************************
*****************************************************
**** DROP NEGATIVE CIP ***
*****************************************************
*****************************************************
sum CIP, d 

drop if CIP < 0 | CIP>r(p99)  //dropping outlier


*****************************************
***** Generate Regression Variables *****
*****************************************
gen ln_pi = ln(pi)	// define allowed amount for visit j as pi


// generate tau and lost revenue 
gen tau = CIP/pi
gen tau_rf = (denied_amt_final/pi)
rename denied_amt_final lost_revenue
rename denied_amt initial_denied_amt

lab var lost_revenue "Final denied amt for a visit"
lab var tau_rf "Tau calculation for each visit if zero resubmission cost"
lab var tau "Tau calculation for each visit"
lab var initial_denied_amt "Amt that is initially denied"
lab var visit_initial_den "At least one line item initially denied"
lab var visit_final_den "At least one line item finally denied"
lab var CIP "CIP at visit level"

// summarize tau 
reg tau if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_IND/parameters/tau_Medicare_pooledreasons.ster"', replace

reg tau if pay_type_pooled ==2
est save `"$output/model_estimation/relax_IND/parameters/tau_Medicaid_pooledreasons.ster"', replace

reg tau if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_IND/parameters/tau_Commercial_pooledreasons.ster"', replace

// summarize tau without resubmission costs 
reg tau_rf if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_IND/parameters/taurf_Medicare.ster"', replace

reg tau_rf if pay_type_pooled ==2
est save `"$output/model_estimation/relax_IND/parameters/taurf_Medicaid.ster"', replace

reg tau_rf if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_IND/parameters/taurf_Commercial.ster"', replace
bys pay_type_pooled: sum CIP lost_revenue, d 


// summarize CIP 
reg CIP if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_IND/parameters/CIP_Medicare_pooledreasons.ster"', replace

reg CIP if pay_type_pooled ==2
est save `"$output/model_estimation/relax_IND/parameters/CIP_Medicaid_pooledreasons.ster"', replace

reg CIP if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_IND/parameters/CIP_Commercial_pooledreasons.ster"', replace

// summarize tau without resubmission costs 
reg lost_revenue if pay_type_pooled ==1 
est save `"$output/model_estimation/relax_IND/parameters/lost_revenue_Medicare.ster"', replace

reg lost_revenue if pay_type_pooled ==2
est save `"$output/model_estimation/relax_IND/parameters/lost_revenue_Medicaid.ster"', replace

reg lost_revenue if pay_type_pooled ==3 
est save `"$output/model_estimation/relax_IND/parameters/lost_revenue_Commercial.ster"', replace
bys pay_type_pooled: sum CIP lost_revenue, d 


erase `"$output/temp/relax_IND_cont_value_model_merged.dta"'





log close 
